package com.hl.wechat.demo.service.impl;

import com.hl.wechat.demo.config.PropertiesConfig;
import com.hl.wechat.demo.config.UrlConfig;
import com.hl.wechat.demo.entity.SystemUser;
import com.hl.wechat.demo.entity.User;
import com.hl.wechat.demo.entity.WxLogin;
import com.hl.wechat.demo.enums.RedisKey;
import com.hl.wechat.demo.redis.RedisService;
import com.hl.wechat.demo.service.UserService;
import com.hl.wechat.demo.websocket.WebSocketManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    RedisTemplate<Object,Object> redisService;
    @Autowired
    PropertiesConfig propertiesConfig;

    @Autowired
    OkHttpServiceImpl okHttpService;

    @Autowired
    WebSocketManager webSocketManager;


    @Override
    public void addUser(User user) {
        user.setLastTimer(System.currentTimeMillis());
        redisService.opsForHash().put(RedisKey.KEY_USER_BEGIN.getValue()+user.getId(), RedisKey.HASH_USER_INFO.getValue(),user);

    }

    @Override
    public boolean isOnline(String openId) {
        return  webSocketManager.isOnline(openId);
    }

    @Override
    public User getUserById(String id) {
        return null;
    }

    @Override
    public User changeUserInfo(User user) {
        return null;
    }

    @Override
    public List<User> listUser() {
        List<User> result =new ArrayList<>();
        /**
         * 查找RedisKey.KEY_USER_BEGIN开头的Key，此开头的key都是用户信息
         */
        Set<Object> keys = redisService.keys(RedisKey.KEY_USER_BEGIN.getValue()+"*");
        keys.forEach(key -> {
            User user =(User) redisService.opsForHash().get(key, RedisKey.HASH_USER_INFO.getValue());
            user.setOnline(isOnline(user.getId()));
            result.add(user);
        });

        result.sort(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return  o1.isOnline()?-1:1;
            }
        });

        return  result;
    }

    @Override
    public WxLogin wxLogin(String jsCode) {
        String url =UrlConfig.WX_LOGIN;
        url =url.replace("{APP_ID}",propertiesConfig.getAppId());
        url =url.replace("{SECRET}",propertiesConfig.getSecret());
        url =url.replace("{JSCODE}",jsCode);
        WxLogin wxLogin = okHttpService.withGet(url, null, WxLogin.class);
        return wxLogin;
    }
}
